﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class ULogin : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Head1.Title = "欢迎登录" + ConfigurationManager.AppSettings["SystemName"];
            SysWord.Text = ConfigurationManager.AppSettings["SystemName"];
        }
    }

    protected void LoginIn_Click(object sender, EventArgs e)
    {
        string sqlStr = "SELECT count(1) FROM u_user_info_t " +
                             "WHERE UPPER(login_name)=UPPER(:login_name) AND login_psd=:login_psd " +
                               "AND if_valid='Y'";
        OracleParameter[] _param ={
			    new OracleParameter(":login_name",OracleType.VarChar,16),
                new OracleParameter(":login_psd",OracleType.VarChar,200)
			};
        _param[0].Value = User_Login.Text.Trim();
        _param[1].Value = Tools.EncryptPasswordByMD5(User_Pass.Text.Trim());
        int count = Int32.Parse(OracleHelper.ExecuteScalar(sqlStr, CommandType.Text, _param).ToString());
        if (count > 0)
        {
            LoginSuccess();            
        }
        else
        {
            LoginFail();
        }
        User_Pass.Text = String.Empty;
    }

    private void LoginSuccess()
    {
        Session["LoginUser"] = User_Login.Text.Trim().ToLower();
        if (User_Pass.Text.Trim().Equals(UserPage.DefaultPass))
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "resetpass"
                , "$(function(){$('#ChangePass').dialog('open');});", true);
        }
        else
        {
            log4net.LogManager.GetLogger(GetType()).Info("{ClientIP:'" + Tools.GetClientIP() + "',Msg:'"
                + Session["LoginUser"].ToString() + " login.'}");
            Response.Redirect("~/UDesktop.aspx");
        }
    }

    private void LoginFail()
    {
        Session["LoginUser"] = null;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "logfail"
            , @"$(function(){alert('用户名或者密码有误！\n注意:密码区分大小写。');});", true);
        User_Pass.Focus();
    }

    protected void BtnSave_Click(object sender, EventArgs e)
    {
        string sqlStr = "UPDATE u_user_info_t SET login_psd =:login_psd WHERE login_name =:login_name";
        OracleParameter[] _params = {
                new OracleParameter(":login_psd",OracleType.VarChar,200),
                new OracleParameter(":login_name",OracleType.VarChar,16)
            };
        _params[0].Value = Tools.EncryptPasswordByMD5(User_Pass_R.Text.Trim());
        _params[1].Value = Session["LoginUser"].ToString();
        OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
        User_Pass_R.Text = "";
        User_Pass_RT.Text = "";
        Page.ClientScript.RegisterStartupScript(GetType(), "resetAlert"
            , "$(function(){alert('密码修改成功，请使用新密码重新登录系统！');});", true);
        User_Pass.Focus();
    }
}
